package com.caiwangdai.app.ui.verify

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.hardware.Camera
import com.caiwangdai.app.R
import com.caiwangdai.app.extension.gone
import com.caiwangdai.app.extension.setOnClickListenerPro
import com.caiwangdai.app.extension.visible
import com.caiwangdai.app.origin.BaseActivity
import com.caiwangdai.app.widget.CameraView
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_id_card_camera.*
import java.io.File

/**
 * 身份证-相机
 */
class IdCardCameraActivity : BaseActivity() {
    override fun getLayoutResID() = R.layout.activity_id_card_camera

    var isRightSize = true//是否为正面身份证
    var currentFile = ""//当前拍照的途径
    var confirming = false;

    companion object {
        val ID_CARD_SIZE = "id_card_size"

        fun newInstance(context: Context, isRightSize: Boolean): Intent {
            val intent = Intent(context, IdCardCameraActivity::class.java)
            intent.putExtra(ID_CARD_SIZE, isRightSize)
            return intent
        }
    }

    override fun initConfig() {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE//请求为横屏幕
    }


    override fun initData() {
        isRightSize = intent.getBooleanExtra(ID_CARD_SIZE, true)
    }

    override fun initView() {
        cameraStatus()

        iv_back.setOnClickListenerPro {
            onBackPressed()
        }
        tv_snap.setOnClickListenerPro {
            camera_card.takePicture()
        }
        camera_mask.setOnClickListenerPro {
            Logger.e("对焦中...")
            camera_card.autoFocus()
        }
        camera_card.onCameraStatusListener = object : CameraView.OnCameraStatusListener {
            override fun onAutoFocus(success: Boolean) {}

            override fun onTouchFocus(mCamera: Camera) {}

            override fun onCameraStopped(url: String) {
                Logger.e("拍照成功=" + url)
                confirmStatus()
                currentFile = url

            }
        }
        //确定
        tv_confirm.setOnClickListenerPro {
            val intent = Intent()
            intent.putExtra("pic", currentFile)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
        //取消
        tv_cancel.setOnClickListenerPro {
            File(currentFile).delete()
            camera_card.onResume()
            cameraStatus()
        }
    }

    override fun onResume() {
        super.onResume()
        if (confirming) return
        camera_card.post {
            camera_card.onResume()
        }
    }

    override fun onPause() {
        super.onPause()
        camera_card.onPause()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        File(currentFile).delete()
    }

    fun confirmStatus() {
        confirming = true
        tv_confirm.visible()
        tv_cancel.visible()
        tv_snap.gone()
    }

    fun cameraStatus() {
        confirming = false
        tv_confirm.gone()
        tv_cancel.gone()
        tv_snap.visible()
    }

}
